import type { ArrangeTemplate } from './arrangeTemplate'
import type { Enrollment } from './enrollment'
import http from '@/utils/http'
import { Lesson } from './lesson'

const BASE_URL = '/education/class'

export interface ClassInfo {
  id: string
  name: string
  classYear: number
  admissionYearLimit: number
  term: string
  grade: string
  subject: string
  classCategory: number
  classTypeId: string
  classLevelLimit: number
  campusId: string
  classroomId: string
  leadTeacherId: string
  assistTeacherId?: string
  totalLessonCount: number
  completedLessonCount: number
  planStudentCount: number
  enrollStudentCount: number
  passedStudentCount: number
  salePerformanceFactor: number
  lessonHourFactor: number
  arrangeTemplateId: string
  timeTemplateId: string
  lessonChangeLimit: number
  totalPrice: number
  unitPrice: number
  saleStatus: number
  classStatus: number
  renewalStartTime: string
  renewalEndTime: string
  enrollStartTime: string
  image?: string
  description?: string
  prevClassId?: string
  remainCount: number
  pendingPayCount: number
}

export interface ClassQuery {
  keyword?: string
  subject?: string
  classYear?: number
  term?: string
  grade?: string[]
  classCategory?: number
  campusId?: string
  classroomId?: string
  classTypeId?: string
  leadTeacherId?: string
  assistTeacherId?: string
  classStatus?: number[]
  saleStatus?: number
}

export interface ClassInfoResp extends ClassInfo {
  campusName: string
  classroomName: string
  leadTeacherName: string
  arrangeTemplate: ArrangeTemplate
}

export interface ClassDetailResp extends ClassInfoResp {
  classTypeName: string
  assistTeacherName: string
}

export interface ClassInfoSummaryResp {
  classCount: number
  planStudentCount: number
  enrollStudentCount: number
  passedStudentCount: number
  totalLessonCount: number
  completedLessonCount: number
}

export interface ClassStudentResp {
  id: string
  studentId: string
  classId: string
  name: string
  avatar: string
  gender: number
  parentPhone: string
  studentJoinType: number
  studentClassStatus: number
  remark: string
  enrollment: Enrollment
}

export interface ClassUpdateReq {
  name?: string
  classYear?: number
  term?: string
  grade?: string
  subject?: string
  classCategory?: number
  admissionYearLimit?: number
  classTypeId?: string
  campusId?: string
  classroomId?: string
  leadTeacherId?: string
  assistTeacherId?: string
  planStudentCount?: number
  prevClassId?: string
  lessonChangeLimit?: number
  saleStatus?: boolean
  totalPrice?: number
  unitPrice?: number
  renewalStartTime?: string
  renewalEndTime?: string
  enrollStartTime?: string
  lessonHourFactor?: number
  salePerformanceFactor?: number
}

export interface ClassLessonQuery {
  lessonStatus?: string | number
  classId?: string
}

export interface ClassLessonResp extends Lesson {
  leadTeacherName: string
  campusName: string
  classroomName: string
  weekName: string

  signInCount: number
  totalCount: number
}

export interface ClassCreateReq {
  name: string
  classYear: number
  term: string
  grade: string
  subject: string
  classCategory: number
  admissionYearLimit: number
  classTypeId: string
  classLevelLimit: number
  campusId: string
  classroomId: string
  leadTeacherId: string
  assistTeacherId?: string
  planStudentCount: number
  arrangeTemplateId: string
  timeTemplateId: string
  lessonChangeLimit: number
  totalPrice: number
  unitPrice: number
  saleStatus: number
  renewalStartTime?: string
  renewalEndTime?: string
  enrollStartTime?: string
  image?: string
  description?: string
  prevClassId?: string
  lessonHourFactor?: number
  salePerformanceFactor?: number

  importPrevStudent?: boolean
}

export interface ClassStudentChangeQuery {
  classId: string | number
  studentId?: string | number
  changeType: string
  operateSource?: string | number
}

export interface ClassStudentListResp {
  lessonId: string

  /* 学生课次id */
  studentLessonId: string

  /* 学生id */
  studentId: string

  /* 学生id */
  studentName: string

  /* enum-学生行课状态,可用值:0,1,2,3,4 */
  studentClassStatus: string

  /* 学生加入方式,可用值:1,2,3 */
  studentJoinType: string
  signInType: string
  /* 签到时间 */
  signInTime: string
  signInStatus: boolean
}

export interface ClassScheduleReq {
  leadTeacherId: string
  campusId: string
  classroomId: string
  arrangeTemplateId: string
  timeTemplateId: string
}

export interface ClassPreScheduleResp {
  lessonIndex: number
  lessonDate: string
  lessonStartTime: string
  lessonEndTime: string
  conflictInfo: object
}

export interface UnpaidStudentResp {
  studentId: string
  avatar: string
  studentName: string
  mobilePhone: string
  orderId: string
  createTime: string
  status: string
  payOff: boolean
  enrollLessonCount: number
}

export const createClass = (data: ClassCreateReq) => {
  return http.post(`${BASE_URL}/create`, data)
}

export const getClassPage = (query: ClassQuery & PageQuery) => {
  return http.get<Page<ClassInfoResp[]>>(`${BASE_URL}/page`, query)
}

export const exportClassInfo = (query: ClassQuery) => {
  return http.download(`${BASE_URL}/export`, query)
}

export function getClassSummary(query: ClassQuery) {
  return http.get<ClassInfoSummaryResp>(`${BASE_URL}/summary`, query)
}

export const getPrevClassList = (query: ClassQuery) => {
  return http.get<ClassInfoResp[]>(`${BASE_URL}/list-prev`, query)
}

export const getClassWithPermissionPage = (query: ClassQuery & PageQuery & { studentId: string }) => {
  return http.get<Page<ClassInfoResp[]>>(`${BASE_URL}/pageWithPermission`, query)
}

export const updateClass = (data: ClassUpdateReq, id: string | number) => {
  return http.post(`${BASE_URL}/update/${id}`, data)
}

export const removeClass = (id: string | number) => {
  return http.post(`${BASE_URL}/delete/${id}`)
}

export const closeClass = (id: string | number) => {
  return http.post(`${BASE_URL}/close/${id}`)
}

export const getClassDetail = (id: string | number) => {
  return http.get(`${BASE_URL}/detail/${id}`)
}

export const getClassStudentList = (id: string) => {
  return http.get<ClassStudentResp[]>(`${BASE_URL}/student/list/${id}`)
}

export const getClassLessonList = (query: ClassLessonQuery) => {
  return http.get<ClassLessonResp[]>(`${BASE_URL}/lesson/list`, query)
}

export const getStudentChangeClassList = (query: ClassStudentChangeQuery & PageQuery) => {
  return http.get<Page<ClassStudentListResp[]>>(`/education/enrollment/change/page`, query)
}

export const generateShecdule = (req: ClassScheduleReq) => {
  return http.post(`${BASE_URL}/lesson/schedule`, req)
}

export const reschedule = (req: ClassScheduleReq, id: string) => {
  return http.post(`${BASE_URL}/lesson/reschedule/${id}`, req)
}

export const updateSaleStatus = (req: { classId: string, status: number }) => {
  return http.post(`${BASE_URL}/update-sale-status`, req)
}

export const importClassStudents = (studentIds: Array<string>, classId: string) => {
  return http.post(`${BASE_URL}/select-import-prev-student/${classId}`, { studentIdList: studentIds })
}

export const getUnpaidStudents = (classId: string) => {
  return http.get(`${BASE_URL}/unpaid-student/${classId}`)
}

export const exportClassStudents = (classId: string) => {
  return http.download(`${BASE_URL}/student/export/${classId}`)
}
